package cn.lzj.org.controller;

import cn.lzj.basic.exception.BusinessException;
import cn.lzj.basic.util.AjaxResult;
import cn.lzj.basic.util.ExcelUtil;
import cn.lzj.basic.util.PageList;
import cn.lzj.org.domain.Shop;
import cn.lzj.org.domain.ShopAuditLog;
import cn.lzj.org.query.ShopQuery;
import cn.lzj.org.service.IShopService;
import cn.lzj.org.vo.ShopDto;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    public IShopService shopService;

    /**
     * 接口：添加或修改
     *
     * @param shop 传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop) {
        try {
            if (shop.getId() != null) {
                shopService.update(shop);
            } else {
                shopService.add(shop);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.util().setmessage("噢哟，系统报错了，我们正在殴打程序员！！!");
        }
    }

    /**
     * 接口：删除
     *
     * @param id
     * @return AjaxResult 响应给前端
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            shopService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.util().setmessage("噢哟，系统报错了，我们正在殴打程序员！！!");
        }
    }

    /**
     * 接口：批量删除
     *
     * @param ids
     * @return AjaxResult 响应给前端
     */
    @PatchMapping
    public AjaxResult removeList(@RequestBody List<Long> ids) {
        try {
            shopService.removeList(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.util().setmessage("噢哟，系统报错了，我们正在殴打程序员！！!");
        }
    }

    /**
     * 接口：查询单个对象
     *
     * @param id
     */
    @GetMapping("/{id}")
    public Shop queryById(@PathVariable("id") Long id) {
        return shopService.queryById(id);
    }


    /**
     * 接口：查询所有
     *
     * @return
     */
    @GetMapping
    public List<Shop> queryAll() {
        return shopService.queryAll();
    }


    /**
     * 接口：分页查询或高级查询
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery query) {
        return shopService.queryPage(query);
    }

    /**
     * 接口：店铺入驻
     *
     * @param
     * @return
     */
    @PostMapping("/settlement")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult settlement(@RequestBody Shop shop) {
        try {
            shopService.settlement(shop);
            return new AjaxResult();//true + "操作成功"
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.util().setmessage("入驻失败，" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.util().setmessage("系统繁忙，请稍后重试!!!");
        }
    }

    /**
     * 接口：店铺审核通过
     * @param
     * @return
     */
    @PostMapping("/audit/pass")
    @ApiOperation(value = "店铺审核通过")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.pass(shopAuditLog);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.util().setmessage("审核失败");        }
    }


   /* *
     * 接口：店铺审核驳回
     * @param
     * @return*/

    @PostMapping("/audit/reject")
    @ApiOperation(value = "店铺审核驳回")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.reject(shopAuditLog);
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.util().setmessage("审核失败");
        }
    }

    /**
     * 文件导出数据
     * @param response
     */
    @GetMapping("/export")
    public void export( HttpServletResponse response){
        try {
            List<Shop> shops = shopService.queryAll();
            ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xlsx", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 报表技术的实现echarts
     * @return
     */
    @GetMapping("/echarts")
    public List<ShopDto> echartsDatas() {
        List<ShopDto> shopDtos = shopService.echartsDatas();
        return shopDtos;
    }
    @GetMapping("/emailpass")
    @ApiOperation(value = "店铺审核驳回")
    //前端传递的是数组 - 后端既可以用数组,也可以用集合
    public AjaxResult emailpass(){
        try {
            shopService.emailpass();
            return new AjaxResult();//true + "操作成功"
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.util().setmessage("审核失败");
        }
    }
}
